package com.aglhz.nature.fragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;



import cn.hugo.android.scanner.CaptureActivity;

import com.aglhz.nature.R;
import com.aglhz.nature.ListView.MyGridView;
import com.aglhz.nature.ListView.MyListView;
import com.aglhz.nature.adapter.ColumnsListAdapter;
import com.aglhz.nature.adapter.HomeRecommendAdapter;
import com.aglhz.nature.modle.item.ColumnsListData;
import com.aglhz.nature.modle.item.RecommendData;
import com.aglhz.nature.presenter.MainPresenter;
import com.aglhz.nature.view.BrandIntroductionActivity;
import com.aglhz.nature.view.SeachActivity;
import com.aglhz.nature.view.Xiu2WebActivity;
import com.aglhz.nature.view.iv.MainView;
import com.aglhz.scrollview.RollHeaderView;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class HomePageFragment extends Fragment implements MainView,OnClickListener{
	public static int CURRENT_IMAGE = 1;

	private static String TAG = HomePageFragment.class.getName();
	private MainPresenter presenter;
	private List<ColumnsListData> columnsListData;
	private ColumnsListAdapter columnsListAdapter;
	private HomeRecommendAdapter homeRecommendAdapter;
	private MyGridView columnsListGridview;
	private RollHeaderView rollHeaderView;
	private MyListView homeRecommend;
	private ImageView xiu1,xiu2,xiu3,xiuTag;
	private ImageView origin;
	
	@Bind(R.id.home_search)
	ImageView home_search;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.homefragment, null);
		rollHeaderView = (RollHeaderView) view
				.findViewById(R.id.rollHeaderView);
		columnsListGridview = (MyGridView) view
				.findViewById(R.id.home_columnsGridView);
		homeRecommend=(MyListView) view.findViewById(R.id.HomeRecommend);
		origin=(ImageView) view.findViewById(R.id.origin);
		xiu1=(ImageView) view.findViewById(R.id.xiu1);
		xiu2=(ImageView) view.findViewById(R.id.xiu2);
		xiu3=(ImageView) view.findViewById(R.id.xiu3);
		xiuTag=(ImageView) view.findViewById(R.id.xiuTag);
		xiu1.setOnClickListener(this);
		xiu2.setOnClickListener(this);
		xiu3.setOnClickListener(this);
		origin.setOnClickListener(this);
		
		
		rollHeaderView
				.setOnHeaderViewClickListener(new RollHeaderView.HeaderViewClickListener() {
					@Override
					public void HeaderViewClick(int position) {
				
						
						Intent intent=new Intent(getActivity(),BrandIntroductionActivity.class);
						intent.putExtra("home_scroll", presenter.getBrindId().get(position));
						startActivity(intent);
					}
				});
		ButterKnife.bind(this, view);
		presenter = new MainPresenter(this);
		// basePresenter unregister
		presenter.register();
		presenter.loadData();

		return view;
	}

	@OnClick(R.id.home_search)
	public void onsearch() {
		Intent intent = new Intent(getActivity(), SeachActivity.class);
		startActivity(intent);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// basePresenter unregister
		presenter.unregister();
	}

	// 设置url数组，展示轮播图
	@Override
	public void showSrcollImage(List<String> urls) {
		Log.i(TAG, "showSrcollView !" + urls);

		rollHeaderView.setImgUrlData(urls);

	}

	// 展示分栏列表
	@Override
	public void setColumnsList(List<ColumnsListData> data) {
		columnsListAdapter = new ColumnsListAdapter(data, getActivity());
		columnsListGridview.setAdapter(columnsListAdapter);

	}
//展示推荐列表
	@Override
	public void seTRecommendList(List<RecommendData> data) {
		homeRecommendAdapter=new HomeRecommendAdapter(data,getActivity());
		homeRecommend.setAdapter(homeRecommendAdapter);
//		setListViewHeightBasedOnChildren(homeRecommend);
	}

	//展示推荐列表2
	@Override
	public void setRecommend2Pic() {
		
		 Picasso.with(getActivity()).load(presenter.setNextTagUrl())
		 .resize(getScreenWidth(), getScreenWidth()/11)
         .into(xiuTag);
		
		 Picasso.with(getActivity()).load(presenter.setUrl1())
		  .resize(getScreenWidth()/2, getScreenWidth()*384/230/2)
         .into(xiu1);
		 
		 Picasso.with(getActivity()).load(presenter.setUrl2())
		  .resize(getScreenWidth()*384/230/4, getScreenWidth()*384/230/4)
         .into(xiu2);
//		 Log.e("getScreenWidth()/2",""+px2dip(getActivity(),getScreenWidth()/2));
		 Picasso.with(getActivity()).load(presenter.setUrl3())
		  .resize(getScreenWidth()*384/230/4,getScreenWidth()*384/230/4)
        .into(xiu3);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.xiu1:
			Intent intent=new Intent(getActivity(),Xiu2WebActivity.class);
			intent.putExtra("xiuNextUrl1",presenter.setNextUrl1());
			Log.e("xiuNextUrl1","---------presenter.setNextUrl1()"+presenter.setNextUrl1());
			startActivity(intent);
			break;
		case R.id.xiu2:
			Intent intent1=new Intent(getActivity(),Xiu2WebActivity.class);
			intent1.putExtra("xiuNextUrl2", presenter.setNextUrl2());
			Log.e("xiuNextUrl2","---------presenter.setNextUrl2()"+presenter.setNextUrl2());
			startActivity(intent1);
			break;
		case R.id.xiu3:
			Intent intent2=new Intent(getActivity(),Xiu2WebActivity.class);
			intent2.putExtra("xiuNextUrl3", presenter.setNextUrl3());
			Log.e("xiuNextUrl3","---------presenter.setNextUrl3()"+presenter.setNextUrl3());
			startActivity(intent2);
			break;
		case R.id.origin:
			Intent intent3=new Intent(getActivity(),CaptureActivity.class);			
			startActivity(intent3);
			break;
		
			
		default:
			break;
		}

		
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @return
	 */
	protected int getScreenWidth() {
		WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		return display.getWidth();
		
	}
	/** 
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
     */  
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  

}
